-- Copyright (c) 2025 Huawei Technologies Co., Ltd.
-- openUBMC is licensed under Mulan PSL v2.
-- You can use this software according to the terms and conditions of the Mulan PSL v2.
-- You may obtain a copy of Mulan PSL v2 at: http://license.coscl.org.cn/MulanPSL2
-- THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
-- EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
-- MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
-- See the Mulan PSL v2 for more details.

local class     = require 'mc.class'
local singleton = require 'mc.singleton'
local log       = require 'mc.logging'
local base_msg  = require 'messages.base'
local enum      = require 'class.types.types'

local RET_ERR = -1
local RET_OK  = 0

local spdm_auth = class()

function spdm_auth:ctor(responder_collection, spdm_c)
    self.m_responder_collection = responder_collection
    self.m_spdm_c = spdm_c
end

function spdm_auth:init()
end

function spdm_auth:do_challenge_auth(chassis_id, device_name)
    local res, mask = self.m_spdm_c:get_digests(device_name)
    if res == RET_ERR then
        self.m_spdm_c:free_spdm_ctx(device_name)
        log:error("get certificate digests failed")
        error(base_msg.InternalError())
    end

    local ok, cert_info
    for i = 0, 7 do -- 0~7共8个槽位
        if (mask >> i) & 1 == 1 then
            ok, cert_info = pcall(function()
                self.m_spdm_c:get_certificate(device_name, i)
            end)
            if not ok then
                log:error("get certificate %s failed, error: %s", i + 1, tostring(cert_info))
            else
                self.m_responder_collection:add_certificate(chassis_id, device_name, i, cert_info)
            end
        end
    end

    -- 默认就使用slot 0的证书进行挑战
    res = self.m_spdm_c:challenge_auth(device_name, 0)
    if res ~= RET_OK then
        log:error("challenge_auth failed")
        error(base_msg.InternalError())
    end
end

function spdm_auth:do_init_measurements(responder)
    local chassis_id = responder.m_chassis_id
    local device_name = responder.m_device_name
    -- 默认就使用slot 0的证书进行测量计算
    local res = self.m_spdm_c:init_measurements(device_name, 0)
    if res ~= RET_OK then
        log:error("init measurements failed")
        error(base_msg.InternalError())
    end

    -- 获取component_integrity和measurement_set
    local integrity = self.m_spdm_c:get_component_integrity(device_name)
    local measurement_set = self.m_spdm_c:get_measurement_set(device_name);
    local integrity_id = self.m_responder_collection:new_integrity(chassis_id, device_name, integrity, measurement_set)

    -- 获取measurement
    local measurement
    local component_integrity = responder.m_integrity_collection[integrity_id]
    local measurements_count = component_integrity.m_measurement_info.measurements_count
    if measurements_count == 0 then
        log:notice("no measurements")
        return
    end

    for i = 1, measurements_count do
        measurement = self.m_spdm_c:get_one_measurement(device_name, i)
        self.m_responder_collection:add_measurement(chassis_id, device_name, integrity_id, measurement)
    end
end

-- responder和
function spdm_auth:do_auth(chassis_id, device_name)
    local responder = self.m_responder_collection.m_collection[chassis_id][device_name]
    if not responder or not responder.m_device_name then
        log:error("can not get responder")
        error(base_msg.InternalError())
    end

    local res = self.m_spdm_c:new_spdm_ctx(device_name)
    res = res | self.m_spdm_c:init_spdm_ctx(device_name, enum.TRANSFER_TYPE.MCTP:value())
    res = res | self.m_spdm_c:check_spdm_ctx(device_name)
    if res ~= RET_OK then
        self.m_spdm_c:free_spdm_ctx(device_name)
        log:error("prepare spdm context failed")
        error(base_msg.InternalError())
    end

    res = self.m_spdm_c:init_connection(device_name)
    if res == RET_ERR then
        self.m_spdm_c:free_spdm_ctx(device_name)
        log:error("init spdm connection failed")
        error(base_msg.InternalError())
    end

    self:do_challenge_auth(chassis_id, device_name)
    self:do_init_measurements(responder)
end

return singleton(spdm_auth)